package com.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import  javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DesUtil {

    public final static String desKey="ecb31a72fdb9a1f8b5ded06";
    /**
     * 加密
     *
     * @param content 需要加密的内容
     * @return
     */
    public static String encrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance("DES");
            DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE,secretKey);
            byte[] byteContent = content.getBytes();
            byte[] result = cipher.doFinal(byteContent);
            return parseByte2HexStr(result); // 加密
        }catch (Exception e){
            e.printStackTrace();
          //  log.error(" encrypt error! passwd is  "+content+" encrypt is des  "+" des key is "+desKey);
        }
        return null;
    }


    /**解密
     * @param content  待解密内容
     * @return
     */
    public  static  String decrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance("DES");
            System.out.println("--desKey---:"+desKey);
            DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.DECRYPT_MODE,secretKey);
            byte[] result = cipher.doFinal(parseHexStr2Byte(content));
            return  new String(result);
        } catch (Exception e) {
            e.printStackTrace();
            //log.error(" decrypt error! passwd is  "+content+" decrypt is des  "+" des key is "+desKey);
        }
        return null;
    }



    /**将二进制转换成16进制
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }


    /**将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static String  base64Encode(String str) throws Exception {

        BASE64Encoder encoder =new BASE64Encoder();
        String result=  encoder.encode(str.getBytes("UTF-8"));
        return  result;

    }

    public static String  base64Decode(String str) throws Exception {
        BASE64Decoder decoder =new BASE64Decoder();
        byte[] result=decoder.decodeBuffer(str);
        String strdec=  new String(result);
        return strdec;
    }


    public static void main(String[] args) {
        DesUtil desUtil =new DesUtil();
        String str =desUtil.encrypt("123456");
        System.out.println(str);
        System.out.println(desUtil.decrypt("43814F8D9BD31434A4B435BA5D6E8386"));

    }
}
